<?php
/**
 *
 * 数据链接
 *
 * @author		Sinxia.com 盛夏科技
 */

class DB {

	private $handle;
	private $is_log=0;//关闭日志

	/*
    * ------------------------------------------------------
    *  配置
    * ------------------------------------------------------
    */

	public $DB_HOST = DB_HOST;
	public $DB_PORT='3306';
	public $DB_USER = DB_USER; //数据库用户名
	public $DB_PWD = DB_PWD; //数据库密码
	public $DB_NAME = DB_NAME; //数据库名称
	public $DB_CHARSET = "utf8";//数据库编码

	private $isConnected = FALSE;
	private $pdo = NULL;

	public $rows_count; //记录条数

	public function __construct()
	{
		$this->connect();
	}

	public function connect()
	{
        //dump(new PDO("mysql:host=10.0.19.127;port=3306;dbname=hotwind_xxl,root,hotWind"));
		if($this->pdo == NULL){
			try {
				$this->pdo = new PDO("mysql:host=".$this->DB_HOST.":".$this->DB_PORT.";dbname=".$this->DB_NAME,$this->DB_USER,$this->DB_PWD);
			}catch( PDOException $e ){
				die( $e->getMessage() );
			}
			$this->pdo->query("SET NAMES ".$this->DB_CHARSET);
			$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
		}
	}

	public function getPdo(){
		return $this->pdo;
	}

	//==================== ↓  by 章斌  ↓ ==================================================


	//获取一条记录
	public function get_one($sql,$result_type = PDO::FETCH_ASSOC) {
		$query = $this->pdo->query($sql);
		$query->setFetchMode($result_type);
		$rt = $query->fetch();
		//$this->write_log("获取一条记录 ".$sql);
		return $rt;
	}
	//获取全部记录
	public function get_all($sql,$result_type = PDO::FETCH_ASSOC) {
		$query = $this->pdo->query($sql);
		$query->setFetchMode($result_type);
		$rt = $query->fetchAll();
		$this->rows_count = $query->rowcount();
		//$this->write_log("获取全部".$this->rows_count."条记录 ".$sql);
		return $rt;
	}

	//添加数据
	public function insert($table,$dataArray) {
		$field = "";
		$value = "";
		if( !is_array($dataArray) || count($dataArray)<=0) {
			//$this->write_log('没有要插入的数据');
			return false;
		}
		while(list($key,$val)=each($dataArray)) {
			$field .="$key,";
			$value .= ($val === NULL || $val === null)? "NULL," : "'$val',";
		}
		$field = substr( $field,0,-1);
		$value = substr( $value,0,-1);
		$sql = "insert into $table($field) values($value)";
		//$this->write_log("插入 ".$sql);
		if(!$this->pdo->exec($sql)) return false;
		return true;
	}

	//By 他他 插入时只更新部分数据
	public function insert_update($table,$insertArray,$updateArray) {
		//插入数组检查
		$field = "";
		$value = "";
		if( !is_array($insertArray) || count($insertArray)<=0) {
			return false;
		}
		while(list($key,$val)=each($insertArray)) {
			$field .="$key,";
            $value .= ($val === NULL || $val === null)? "NULL," : "'$val',";
		}
		$field = substr( $field,0,-1);
		$value = substr( $value,0,-1);

		//更新数组检查
		$update_value = "";
		if( !is_array($updateArray) || count($updateArray)<=0) {
			return false;
		}
		while( list($update_key,$update_val) = each($updateArray))
			$update_value .= "$update_key = '$update_val',";
		$update_value .= substr( $update_value,0,-1);

		$sql = "insert into $table($field) values($value) ON DUPLICATE KEY UPDATE $update_value";
		if(!$this->pdo->exec($sql)) return false;
		return true;
	}

	//更新
	public function update( $table,$dataArray,$condition="") {
		if( !is_array($dataArray) || count($dataArray)<=0) {
			//$this->write_log('没有要更新的数据');
			return false;
		}
		$value = "";
		while( list($key,$val) = each($dataArray))
			$value .= ($val === NULL || $val === null)? "$key = NULL," : "$key = '$val',";
		$value .= substr( $value,0,-1);
		$sql = "update $table set $value where 1=1 and $condition";
		$this->write_log("更新 ".$sql);
		if(!$this->pdo->query($sql)) return false;
		return true;
	}

	//删除
	public function delete( $table,$condition="") {
		if( empty($condition) ) {
			//$this->write_log('没有设置删除的条件');
			return false;
		}
		$sql = "delete from $table where 1=1 and $condition";
		//$this->write_log("删除 ".$sql);
		if(!$this->pdo->query($sql)) return false;
		return true;
	}

	//获取最后插入的id  //TAE线上不支持
	public function insert_id() {
		$id = $this->pdo->lastInsertId();
		//$this->write_log("最后插入的id为".$id);
		return $id;
	}

	//写入日志文件
	public function write_log($msg=''){
		if($this->is_log){
			$text = date("Y-m-d H:i:s")." ".$msg."\r\n";
			$file = fopen(BASE_PATH."/logs/dblog.txt","a");
			fwrite($file,$text);
			fclose($file);
		}
	}


}